package yxy.cherry.battle.module.affix.buff;

import yxy.cherry.battle.Battle;
import yxy.cherry.battle.Fighter;
import yxy.cherry.battle.module.PropertyType;
import yxy.cherry.battle.module.affix.AffixContext.AffixTarget;
import yxy.cherry.battle.module.affix.AffixFactory.BuffTypeEnum;
import yxy.cherry.battle.module.affix.Buff;
import yxy.cherry.battle.module.affix.BuffType;
import yxy.cherry.battle.module.record.stream.RecordStream43Shield;
import yxy.cherry.battle.module.record.stream.RecordStream_BFA;
import yxy.cherry.data.bean.skill;

import java.util.ArrayList;

/**
 * @author James
 * 属性盾（护盾）
 * @date 2022/2/24 19:09
 */

@BuffType(Type = BuffTypeEnum.护盾)
public class Affix62AttributeShield extends Buff {
	long 护盾值 = 0;

	private PropertyType getHPShieldType() { return PropertyType.forNumber(getParam(0));}
	private int get护盾比例() {
		return getParam(1);
	}

	public Affix62AttributeShield(Fighter source, Fighter target, skill skilldata, int[] params) {
		super(source, target, skilldata, params);
		护盾值 = source.getData().getPropertyData().getValue(getHPShieldType()) * get护盾比例() / VALUE_比率分母;
	}

	@Override
	public boolean exec(Fighter source, Fighter actor) {
		AffixTarget affixTarget = getAffixAction().getTarget(actor);

		ArrayList<Long> 伤害流 = affixTarget.get伤害流();
		ArrayList<Long> 效果流 = affixTarget.get效果流();
		ArrayList<Long> 结算伤害流 = new ArrayList<>();
		ArrayList<Long> 结算效果流 = new ArrayList<>();
		long sDamage = 0L;
		for (int i = 0;i<伤害流.size();i++) {
			long 伤害 = 伤害流.get(i);
			long 效果;
			if (护盾值 <= 伤害) {
				if(护盾值 == 0){
					效果 = 效果流.get(i);
				}else{
					效果 = EFFECT_SHIELD;
				}
				sDamage = 护盾值;
				伤害 -= 护盾值;
				护盾值 = 0;
			} else {
				护盾值 -= 伤害;
				sDamage += 伤害;
				伤害 = 0L;
				效果 = EFFECT_SHIELD;
			}
			结算伤害流.add(伤害);
			结算效果流.add(效果);
		}
		affixTarget.set伤害流(结算伤害流);
		affixTarget.set效果流(结算效果流);

		if(sDamage>0){getRecord().stream(new RecordStream43Shield(affixTarget.get目标(),sDamage)).record();}

		if (护盾值 <= 0) {
			ArrayList<Buff> buffList = new ArrayList<>();
			buffList.add(actor.getBuffMap().get(getBuffCodeSeq()));
			actor.removeBuff(actor.getBuffMap().get(getBuffCodeSeq()));
			getRecord().stream(new RecordStream_BFA(actor, buffList)).record();
			Battle.battleLogger(6,"属性盾："+actor.getName()+"护盾被打没了");
		}

		return true;
	}

	@Override
	public BuffType getBuffType() {
		return BuffType.增益效果;
	}

	@Override
	public BuffStateType getBuffStateType() {
		return BuffStateType.attributeShieldState;
	}
}